package dangnhap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.LookAndFeel;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceMistSilverLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceModerateLookAndFeel;

import trangchu.TrangChu;
import javax.swing.JCheckBox;

public class FromDangNhap extends JFrame implements ActionListener {

	private JPanel plNorth,plCenter,plSouth,pl;
	private JLabel lblTaiKhoan;
	private JTextField txtTaiKhoan;
	private JLabel lblMatKhau;
	private JTextField txtMatKhau;
	private JButton btnDangNhap;
	private JButton btnKetThuc;
	private JLabel lblDangNhap;
	private JCheckBox CheckBox;
	static ProgressMonitor monitor;
	public static String rootPath=null;
	  static int progress;

	  static Timer timer;

	  static class ProgressMonitorHandler implements ActionListener {
	    
	    public void actionPerformed(ActionEvent ev) {
	      if (monitor == null)
	        return;
	      if (monitor.isCanceled()) {
	        System.out.println("Monitor canceled");
	        timer.stop();
	      } else {
	        progress += 3;
	        monitor.setProgress(progress);
	        monitor.setNote("Loading... " + progress + " %");
	      }
	    }
	  }
	  
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame.setDefaultLookAndFeelDecorated(true);
                LookAndFeel laf = new SubstanceModerateLookAndFeel();
                SubstanceLookAndFeel.setToUseConstantThemesOnDialogs(true);
                //UIManager.put(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,Boolean.TRUE);
                UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS, Boolean.TRUE);
                JFrame.setDefaultLookAndFeelDecorated(false);
                JDialog.setDefaultLookAndFeelDecorated(true);
                try {
                        UIManager.setLookAndFeel(laf);
                        FromDangNhap frame = new FromDangNhap();
    					frame.setVisible(true);
                } catch (UnsupportedLookAndFeelException e) {
                        e.printStackTrace();
                }
            }
        });
	}
	
	public FromDangNhap() {
		FromDangNhap.rootPath = this.getClass().getResource("/").getPath();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Phần mềm quản lý bán hàng");
		Image icon = Toolkit.getDefaultToolkit().getImage(FromDangNhap.rootPath+"images/TrangChu.gif");
        this.setIconImage(icon); 
		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	    setBounds((screenSize.width-350)/2, (screenSize.height-200)/2, 350, 200);
		getContentPane().setLayout(new BorderLayout(0, 0));
		plNorth = new JPanel();
		getContentPane().add(plNorth,BorderLayout.NORTH);
		plNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
		lblDangNhap = new JLabel("Đăng Nhập");
		lblDangNhap.setBounds(10, 2, 400, 30);
		lblDangNhap.setFont(new Font("Tahoma", 2, 12));
		lblDangNhap.setFont(new Font("Tahoma", 3, 18));		
		lblDangNhap.setForeground(Color.blue);
		plNorth.add(lblDangNhap);
		
		pl = new JPanel();
		
		ImageIcon im = new ImageIcon(FromDangNhap.rootPath+"images/login.gif");
		JLabel lblImage = new JLabel(im);
		pl.add(lblImage);
		getContentPane().add(pl,BorderLayout.WEST);
		
		plCenter = new JPanel();
		getContentPane().add(plCenter,BorderLayout.CENTER);
		plCenter.setLayout(new MigLayout("", "[][][grow]", "[][][][]"));
		
		lblTaiKhoan = new JLabel("Tài Khoản");
		plCenter.add(lblTaiKhoan, "cell 1 1,alignx trailing");
		
		txtTaiKhoan = new JTextField();
		plCenter.add(txtTaiKhoan, "cell 2 1,growx");
		txtTaiKhoan.setColumns(10);
		
		lblMatKhau = new JLabel("Mật Khẩu");
		plCenter.add(lblMatKhau, "cell 1 2,alignx trailing");
		
		txtMatKhau = new JPasswordField();
		plCenter.add(txtMatKhau, "cell 2 2,growx");
		txtMatKhau.setColumns(10);
		
		CheckBox = new JCheckBox("Nhớ tài khoản và mật khẩu");
		plCenter.add(CheckBox, "cell 2 3");
		
		plSouth = new JPanel();
		getContentPane().add(plSouth,BorderLayout.SOUTH);
		plSouth.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
		
		btnDangNhap = new JButton("Đăng Nhập",new ImageIcon(FromDangNhap.rootPath+"/images/dangnhap.gif"));
		plSouth.add(btnDangNhap);
		
		btnKetThuc = new JButton("Kết Thúc",new ImageIcon(FromDangNhap.rootPath+"/images/xoa1.gif"));
		plSouth.add(btnKetThuc);
		
		//su kien
		btnDangNhap.addActionListener(this);
		btnKetThuc.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnKetThuc){
			dispose();
		}
		if(e.getSource() == btnDangNhap){
			//if(txtTaiKhoan.getText().equals("admin") & txtMatKhau.getText().equals("123") ){
//				monitor = new ProgressMonitor(this.btnDangNhap, "Đang load dữ liệu...","Getting Started...", 0, 100);
//		          progress = 0;
//		          if (monitor != null) {	        	
//			          if (timer == null) {
//			            timer = new Timer(100, new ProgressMonitorHandler());
//			          }
//			          timer.start();
//			        }
				dispose();
				new ProgressBarDemo().setVisible(true);

		         // new TrangChu(this).setVisible(true);
				
//			}else{
//				JOptionPane.showMessageDialog(btnKetThuc, "Tài khoản hoặc mật khẩu sai !\n Vui lòng nhập lại.","Lỗi đăng nhập",JOptionPane.OK_OPTION);
//			}
		}
		
	}

}
